package org.logMonitor.web;

import org.logMonitor.dto.ChinaMap;
import org.logMonitor.dto.Map2city;
import org.logMonitor.entity.Rule;
import org.logMonitor.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/1/21.
 */
@Controller
@RequestMapping("/rule")//url:模块/资源/{}/细分
public class RuleController {
    @Autowired
    private RuleService ruleService;
    /**
     * 页面跳转到模拟迁移地图
     * @param model
     * @return
     */
    @RequestMapping(value = "/map",method = RequestMethod.GET)
    public  String chinaMap (Model model){
        int ruleId = 4;
        int ipType = 1;//1是由一个城市发往多个，源ip固定；0是由多个城市发往一个城市，目的ip固定
        model.addAttribute("ruleId",ruleId);
        model.addAttribute("ipType",ipType);
        return "chinaMap";
    }
    /**
     * 页面跳转到模拟迁移地图
     * @param model
     * @return
     */
    @RequestMapping(value = "/mapPlus/{ruleId}",method = RequestMethod.GET)
    public  String chinaMapPlus (Model model,@PathVariable("ruleId") int ruleId){
        int ipType ;//1是由一个城市发往多个，源ip固定；0是由多个城市发往一个城市，目的ip固定
        Rule rule = ruleService.getRuleById(ruleId);
        if(rule != null){
            if(rule.getType().equals("0")){
                ipType=0;//0是由多个城市发往一个城市，目的ip固定
            }else{
                ipType=1;//1是由一个城市发往多个，源ip固定
            }
        }else{
            ipType=1;
        }
        model.addAttribute("ruleId",ruleId);
        model.addAttribute("ipType",ipType);
        model.addAttribute("rule",rule);
        return "chinaMapPlus";
    }
    /**
     * 展示模拟迁移地图
     * @return
     */
    @RequestMapping(value = "/chinaMapInfo",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    //自动转为json
    @ResponseBody
    public Map2city getChinaMapInfo(int ruleId){
        Map2city chinaMaps=new Map2city();
        try{
            chinaMaps = ruleService.getChinaMapDateByRuleId(ruleId);
        }catch (Exception e){

        }
        return  chinaMaps;
    }

    @RequestMapping(value = "/showRules")
    public String showRules(Model model){
        List<Rule> ruleList = new ArrayList<Rule>();
        ruleList = ruleService.getAllRule();
        model.addAttribute("ruleList",ruleList);
        return "rules";
    }
    @RequestMapping(value = "/updateRule",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    //自动转为json
    @ResponseBody
    public String updateRule(int ruleId,boolean bool){
        try{
            if(bool){
                ruleService.openRule(ruleId);
            }else{
                ruleService.closeRule(ruleId);
            }
        }catch (Exception e ){
            return "失败";
        }
        return  "成功";
    }


    @RequestMapping(value = "/deleteRule/{ruleId}",method = RequestMethod.GET)
    public String deleteRules(Model model,@PathVariable("ruleId") int ruleId){
        ruleService.deletRule(ruleId);
        return "redirect:/rule/showRules";
    }
    @RequestMapping(value = "/toAddRule",method = RequestMethod.GET)
    public String toAddRule(){
        return "addRule";
    }

    @RequestMapping(value = "/addRule",method = RequestMethod.POST)
    public String addRule(Rule rule){
        rule.setExist(true);
        rule.setValid(true);
        if(rule.getSrc_ip()!=null&&rule.getDst_ip()!=null&&!rule.getSrc_ip().equals("")&&!rule.getDst_port().equals("")){
            rule.setType("1");//1是源ip固定&&目的ip固定
        }else if(rule.getDst_ip()!=null&&!rule.getDst_port().equals("")){
            rule.setType("0");//0是由多个城市发往一个城市，目的ip固定
        }else{
            rule.setType("1");//1是由一个城市发往多个，源ip固定；0是由多个城市发往一个城市，目的ip固定
        }
        ruleService.addRule(rule);
        return "redirect:/rule/showRules";
    }

    @RequestMapping(value = "/addRuleFast",method = RequestMethod.POST)
    public String addRuleFast(Rule rule){
        rule.setExist(true);
        rule.setValid(true);
        rule.setPro("");
        rule.setDst_port("");
        rule.setSrc_port("");
        int ruleId=1;
        ruleService.addRule(rule);
        ruleId=rule.getId();
        return "redirect:/rule/mapPlus/"+ruleId;
    }
}
